<template>
  <div class="login-container">
    <el-form :model="user" :rules="rules1" ref="ruleForm2" label-position="left" label-width="0px"
             class="demo-ruleForm login-page">
      <h3 class="title">广告位管理系统·登录</h3>
      <el-form-item prop="account">
        <el-input type="text" v-model="user.account" auto-complete="off" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item prop="checkPass">
        <el-input type="password" v-model="user.password" auto-complete="off" placeholder="请输入密码" @keyup.enter.native="handleSubmit"></el-input>
      </el-form-item>
      <el-form-item user-login-other>
        <router-link style="float: left" :to="{path:'/findPassword'}">
          <el-link :underline="false" type="primary">忘记密码</el-link>
        </router-link>
        <router-link style="float: right" :to="{path:'/register'}">
          <el-link :underline="false" type="primary">注册账号</el-link>
        </router-link>
      </el-form-item>
      <el-form-item style="width:100%; text-align: right">
        <el-button type="primary" style="width:100%;" @click.native.prevent="handleSubmit" >
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import loginApi from '@/api/login/login'

export default {
  name: 'login',
  data () {
    return {
      user: {
        account: '', // 用户账号
        password: '' // 用户密码
      },
      rules1: { // 表单验证规则
        account: [
          {required: true, message: '请输入账号', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ]
      },
      checked: true
    }
  },
  methods: {
    /**
     * 登陆表单提交按钮点击事件，将账号、密码作为参数传给api来判断是否可以登录
     * 如果可以则跳到登陆后首页
     */
    handleSubmit () {
      this.$refs.ruleForm2.validate((valid) => {
        if (valid) {
          loginApi.login(this.user.account, this.user.password).then(res => {
            if (res === null) {
              alert('账号或者密码错误')
            } else {
              this.$store.commit('setState', {role: res.roleCode, isLogin: 'true', accountId: res.userId, accountName: this.user.account})
              this.$router.push({path: '/'})
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
  .login-container {
    width: 100%;
    height: 100%;
  }

  .login-page {
    -webkit-border-radius: 5px;
    border-radius: 5px;
    margin: 180px auto;
    width: 350px;
    padding: 20px 35px 15px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }
</style>
